package frontend.user.controller;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import core.address.model.BasicAddress;
import core.profile.model.BasicCredential;
import core.profile.model.BasicProfile;
import core.profile.model.BasicProfileAv;
import frontend.user.dao.EditProfileDao;

public class UserProfileAction extends ActionSupport {
	
	private static final long serialVersionUID = -4008768000656606508L;
	private String title;
	private String firstname;
	private String lastname;
	private String secondname;
	private String passwordreminder;
	private String reminderemail;
	private String mobile;
	private String phone;
	private String phonebusiness;
	private String companyname;
	private String addressLine1;
	private String city1 ;
	private String state1 ;
	private String postalcode;
	private String country1 ;
	private Long uuid ;
	private BasicProfile basicProfile;
	private BasicProfile basicProfile1;
	private BasicAddress basicAddress;
	private BasicCredential basicCredential;
	
	public String getFirstname() {
		return firstname;
	}
	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}
	public String getLastname() {
		return lastname;
	}
	public void setLastname(String lastname) {
		this.lastname = lastname;
	}
	public String getMobile() {
		return mobile;
	}
	public void setMobile(String mobile) {
		this.mobile = mobile;
	}
	public String getSecondname() {
		return secondname;
	}
	public void setSecondname(String secondname) {
		this.secondname = secondname;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getPhonebusiness() {
		return phonebusiness;
	}
	public void setPhonebusiness(String phonebusiness) {
		this.phonebusiness = phonebusiness;
	}
	public String getPostalcode() {
		return postalcode;
	}
	public void setPostalcode(String postalcode) {
		this.postalcode = postalcode;
	}
	public String getCompanyname() {
		return companyname;
	}
	public void setCompanyname(String companyname) {
		this.companyname = companyname;
	}
	public String getAddressLine1() {
		return addressLine1;
	}
	public void setAddressLine1(String addressLine1) {
		this.addressLine1 = addressLine1;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public Long getUuid() {
		return uuid;
	}
	public void setUuid(Long uuid) {
		this.uuid = uuid;
	}
	public String getPasswordreminder() {
		return passwordreminder;
	}
	public void setPasswordreminder(String passwordreminder) {
		this.passwordreminder = passwordreminder;
	}
	public String getReminderemail() {
		return reminderemail;
	}
	public void setReminderemail(String reminderemail) {
		this.reminderemail = reminderemail;
	}
	public String getCity1() {
		return city1;
	}
	public void setCity1(String city1) {
		this.city1 = city1;
	}
	public String getState1() {
		return state1;
	}
	public void setState1(String state1) {
		this.state1 = state1;
	}
	public String getCountry1() {
		return country1;
	}
	public void setCountry1(String country1) {
		this.country1 = country1;
	}
	public BasicProfile getBasicProfile() {
		return basicProfile;
	}
	public void setBasicProfile(BasicProfile basicProfile) {
		this.basicProfile = basicProfile;
	}
	public BasicAddress getBasicAddress() {
		return basicAddress;
	}
	public void setBasicAddress(BasicAddress basicAddress) {
		this.basicAddress = basicAddress;
	}
	
	public BasicCredential getBasicCredential() {
		return basicCredential;
	}
	public void setBasicCredential(BasicCredential basicCredential) {
		this.basicCredential = basicCredential;
	}
	public String  execute() throws Exception{
		
		Map<String, Object> session = ActionContext.getContext().getSession();
		if(session.get("uuid")!=null){
		basicProfile=GetDataByEmailId.listProfileById((Long) session.get("uuid"));
		basicCredential=GetDataByEmailId.getBasicCredentialByUserId(session.get("emailId").toString());
		return SUCCESS;
		}
		else{
			return ERROR;
		}
		
	}
	
	public String updateUserProfile(){
		
		Map<String, Object> session = ActionContext.getContext().getSession();
		if(session.get("uuid")!=null){
		basicCredential=GetDataByEmailId.getBasicCredentialByUserId(session.get("emailId").toString());
		basicProfile1=GetDataByEmailId.listProfileById((Long) session.get("uuid"));
		BasicAddress currentAddress = basicProfile1.getCurrentAddress();
		if(currentAddress == null)
		{
			currentAddress = new BasicAddress();
		}
		currentAddress.setAddressLine1(basicAddress.getAddressLine1());
		currentAddress.setCity1(basicAddress.getCity1());
		currentAddress.setState1(basicAddress.getState1());
		currentAddress.setCountry1(basicAddress.getCountry1());
		currentAddress.setPostalcode(basicAddress.getPostalcode());
		currentAddress.setMobile(basicAddress.getMobile());
		currentAddress.setPhone(basicAddress.getPhone());
		currentAddress.setPhonebusiness(basicAddress.getPhonebusiness());
		basicProfile1.setTitle(basicProfile.getTitle());
		basicProfile1.setFirstname(basicProfile.getFirstname());
		basicProfile1.setSecondname(basicProfile.getSecondname());
		basicProfile1.setLastname(basicProfile.getLastname());
		basicProfile1.setCompanyname(this.getCompanyname());
		basicCredential.setPasswordreminder(this.getPasswordreminder());
		basicCredential.setReminderemail(this.getReminderemail());
		EditProfileDao.saveAddress(currentAddress);
		basicProfile1.setCurrentAddress(currentAddress);
		EditProfileDao.updateBasicCredential(basicCredential);
		EditProfileDao.editProfile(basicProfile1);
		
		
		return SUCCESS;
		}else{
			return ERROR;
		}
	}

}
